﻿using System;

namespace NATP2P.UDPNat
{
    class NATHeader
    {

        private int _messageType;

        public int MessageType
        {
            get { return _messageType; }
            set { _messageType = value; }
        }

        private bool _eHeader;

        public bool EHeader
        {
            get { return _eHeader; }
            set { _eHeader = value; }
        }

        private int _nPacket;

        public int NPacket
        {
            get { return _nPacket; }
            set { _nPacket = value; }
        }
        private UInt16 _port;

        public UInt16 Port
        {
            get { return _port; }
            set { _port = value; }
        }

        public NATHeader()
        {

        }
        public byte[] ToByte(int MessageType, bool EH, int NPacket, int DPort)
        {
            byte[] header = new byte[4];
            header[0] = (byte)(Convert.ToByte(EH) + ((MessageType << 1) & 0xFF));
            header[1] = (byte)(NPacket & 0xFF);
            header[2] = (byte)(DPort & 0xFF);
            header[3] = (byte)(DPort >> 8);

            return header;
        }
        public void Parse(byte[] data)
        {
            _eHeader = Convert.ToBoolean(data[0] & 0x1);
            _messageType = (data[0] >> 1);
            _nPacket = data[1];
            _port = (UInt16)((data[2] & 0xFF) + (data[3] << 8));

        }

    }
}
